﻿#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__


#ifdef _WIN32
#define  TPSTDCALL __stdcall
#else
//#define TPSTDCALL __attribute__((__stdcall__))
#define TPSTDCALL
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif


#define THREADPOOL_LONG_MAX 2147483647L


typedef unsigned int thread_ulong_t;
typedef int thread_bool_t;

typedef void* (TPSTDCALL * queue_hook_cb)(void*);
typedef void (TPSTDCALL * timer_hook_cb)(void* arg, long event);

struct __THREADPOOL;

typedef struct __THREADPOOL* THREADPOOL;
typedef unsigned int THREADTIMER;

typedef struct __HQUEUE__
{
    void* unused;
}* HQUEUE;

typedef struct __THREADPOOL_CONFIG
{
    thread_ulong_t thread_num; //常驻线程
    thread_ulong_t max_thread_num; //最大线程数
    thread_ulong_t queue_max_num; //最大队列元素个数
    thread_ulong_t max_timer_count; //最大定时器个数
    thread_ulong_t priority_count; //队列优先级个数
} THREADPOOL_CONFIG;

typedef unsigned int HQTIMER;


#ifdef __cplusplus
extern "C" {
#endif

THREADPOOL_CONFIG* threadpool_config_new();
void threadpool_config_free(THREADPOOL_CONFIG* config);

THREADPOOL threadpool_create(THREADPOOL_CONFIG* config);
thread_bool_t threadpool_destroy(THREADPOOL pool);
int threadpool_add_job(THREADPOOL pool, int priority, thread_bool_t block_full,
                       queue_hook_cb callback, void* arg);

THREADTIMER threadpool_set_timer(THREADPOOL pool, thread_ulong_t timeout, timer_hook_cb callback, void* arg,
                                 long event);
thread_bool_t threadpool_kill_timer(THREADPOOL pool, THREADTIMER timer);


//one loop per thread
HQUEUE queue_create(unsigned int max_message);
unsigned int queue_dispatch(HQUEUE queue);
int queue_post(HQUEUE queue, queue_hook_cb callback, void* arg);
thread_bool_t queue_break(HQUEUE queue);
thread_bool_t queue_destroy(HQUEUE queue);

HQTIMER queue_set_timer(HQUEUE queue, thread_ulong_t timeout, timer_hook_cb callback, void* arg, long event);
thread_bool_t queue_kill_timer(HQUEUE queue, HQTIMER timer);

#ifdef __cplusplus
};
#endif


#endif
